package com.qingxin.credit.service;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.credit.entity.Credit;
import com.qingxin.park.entity.Company;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

@Service
public class CreditService extends BaseService<Credit, String>{

	
	/**
	 * 列表数据
	 * @param pageParam
	 * @param search
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String search, HttpServletRequest request){
		Querier querier = Querier.create("from Credit where 1=1");
		//按评定单位名称搜索
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (evaluateUnit like :evaluateUnit)");
			querier.setParam("evaluateUnit", search);
		}
		querier.append(pageParam.getOrderby(" order by evaluateTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 添加
	 * @param credit
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Credit credit, LoginUser loginUser, HttpServletRequest request){
		if(StringUtils.isNotEmpty(credit.getInspectAttachment())){
			UploadFile.moveTmpTo(credit.getInspectAttachment(),UploadFile.DIR_CREDIT_ATTACHMENT);
		}
		this.save(credit);
		return null;
	}
	
	/**
	 * 获得所有企业
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Company> getParkData(LoginUser loginUser){
		List<Company> list = listByHql("select new com.qingxin.park.entity.Company(companyid,companyName) from Company");
		if(list == null){
			return new ArrayList<Company>();
		}
		return list;
	}
	
	/**
	 * 删除
	 * @param ids
	 * @param request
	 * @return
	 */
	public SysError delete(String[] ids,HttpServletRequest request){
		for(String id : ids){
			Credit credit = this.get(id);
			if(credit == null){
				continue;
			}
			this.delete(credit);
		}
		return null;
	}
	
	/**
	 * 修改
	 * @param credit
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Credit credit,LoginUser loginUser, HttpServletRequest request){
		Credit old = getSafeEntity(credit.getCreditid());
		if(StringUtils.isNotEmpty(credit.getInspectAttachment()) && !credit.getInspectAttachment().equals(old.getInspectAttachment())){
			UploadFile.moveTmpTo(credit.getInspectAttachment(),UploadFile.DIR_CREDIT_ATTACHMENT);
		}
		String fields = "companyid,companyName,inspectDesc,inspectAttachment,evaluateBy,evaluateDesc,evaluateUnit,evaluateLevel,evaluateTime";
		BeanUtil.setTargetFields(old,credit, fields, true);
		Updater<Credit> updater = new Updater<>(old, fields);
		this.update(updater);		
		return null;
	}
}
